package com.springboot.blog.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.springboot.blog.beans.Blog;
import com.springboot.blog.beans.BlogClassify;
import com.springboot.blog.beans.PageBlog;
import com.springboot.blog.config.utils.R;
import com.springboot.blog.dao.BlogDao;
import com.springboot.blog.dao.ClassifyDao;
import com.springboot.blog.service.BlogService;
import net.sf.json.JSONArray;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class BlogController {

    @Resource
    private BlogDao blogDao;
    @Resource
    private BlogService blogService;
    @Resource
    private ClassifyDao classifyDao;



    @RequestMapping("addBlog")
    public R addBlog(String title,String modules,String status,String content,String blogText){
        Blog blog = new Blog();
        blog.setBlogTitle(title);
        blog.setBlogClassifyId(modules);
        blog.setBlogClassifyName(blogText);
        blog.setBlogContent(content);
        blog.setBlogStatus(Byte.parseByte(status));
        int insert = blogDao.insert(blog);
        if (insert>0){
            return R.ok();
        }
        return R.error();
    }

    @RequestMapping("addUserBlog")
    public R addUserBlog(HttpServletRequest request,String title,String modules,String content,String blogText){
        String userName = (String) request.getSession().getAttribute("userName");
        Blog blog = new Blog();
        blog.setBlogTitle(title);
        blog.setBlogClassifyId(modules);
        blog.setBlogClassifyName(blogText);
        blog.setBlogContent(content);
        blog.setBlogStatus((byte) 0);
        blog.setBlogUserUsername(userName);
        int insert = blogDao.insert(blog);
        if (insert>0){
            return R.ok();
        }
        return R.error();
    }

    @RequestMapping("updateBlog")
    public R updateBlog(String blogId,String title,String modules,String status,String content,String blogText){
        Blog blog = new Blog();
        blog.setBlogId(Integer.parseInt(blogId));
        blog.setBlogTitle(title);
        blog.setBlogClassifyId(modules);
        blog.setBlogClassifyName(blogText);
        blog.setBlogContent(content);
        blog.setBlogStatus(Byte.parseByte(status));
        int update = blogDao.updateById(blog);
        if (update>0){
            return R.ok();
        }
        return R.error();
    }

    @RequestMapping("allBlog")
    private String allBlog(Integer page,Integer limit){

        PageBlog<Blog> pageBlog = blogService.page(page, limit);
        int count = blogDao.selectCount(null);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("count",count);
        jsonObject.put("data",pageBlog);
        return jsonObject.toString();
    }

    @RequestMapping("allUserBlog")
    private String allUserBlog(HttpServletRequest request, Integer page, Integer limit){
        String userName = (String) request.getSession().getAttribute("userName");
        PageBlog<Blog> pageBlog = blogService.pageBlog(page, limit,userName);
        int count = blogDao.findUserBlogCount(userName);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("count",count);
        jsonObject.put("data",pageBlog);
        return jsonObject.toString();
    }

    @RequestMapping("updateBlogStatus")
    public void  updateBlog(String blogId,String blogstatus){
        Blog blog = new Blog();
        blog.setBlogId(Integer.parseInt(blogId));
        blog.setBlogStatus(Byte.parseByte(blogstatus));
        blogDao.updateById(blog);
    }

    @RequestMapping("deleteByIdBlog")
    public R deleteByIdBlog(String blogId){
        int delete = blogDao.deleteById(Integer.parseInt(blogId));
        if (delete>0){
            return R.ok();
        }
        return R.error();
    }

    @RequestMapping("deleteBlog")
    public R deleteBlog(String delete){
        List<Blog> list2=(List<Blog>) JSONArray.toList(JSONArray.fromObject(delete), Blog.class);
        List<Integer> blogList = new ArrayList<>();
        for (Blog blog : list2){
            blogList.add(blog.getBlogId());
        }
        int deleteBlog = blogDao.deleteBatchIds(blogList);
        if (deleteBlog>0) {
            return R.ok();
        }
        return R.error();
    }


}
